{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# C++的运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 运算符作用：用于执行代码的运算。\n",
    "\n",
    "② 运算符类型主要有四种：\n",
    "\n",
    "1. 算术运算符：用于处理四则运算。  \n",
    "2. 赋值运算符：用于将表达式的值赋给变量  \n",
    "3. 比较运算符：用于表达式的比较，并返回一个真值或假值。  \n",
    "4. 逻辑运算法：用于表达式的值返回真值或假值。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 算术运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1 加减乘除运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "int main()\n",
    "{\n",
    "\n",
    "    int a1 = 10;\n",
    "    int b1 = 3;\n",
    "\n",
    "    cout << a1 + b1 << endl;\n",
    "    cout << a1 - b1 << endl;\n",
    "    cout << a1 * b1 << endl;\n",
    "    cout << a1 / b1 << endl;  //两个整数相除，结果依然是整数，将小数部分去除\n",
    "\n",
    "    int a2 = 10;\n",
    "    int b2 = 20;\n",
    "\n",
    "    cout << a2 / b2 << endl;\n",
    "\n",
    "    int a3 = 10;\n",
    "    int b3 = 0;\n",
    "\n",
    "    //cout << a3 / b3 << endl;  //两个数相除，除数不可以为0\n",
    "\n",
    "    //两个小数可以相除\n",
    "    double d1 = 0.51;        //如果d1为0.5，运算结果为整数2\n",
    "    double d2 = 0.25;\n",
    "    \n",
    "    cout << d1 / d2 << endl; //运算结果也可以是小数\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 13  \n",
    " - 7  \n",
    " - 30  \n",
    " - 3  \n",
    " - 0  \n",
    " - 2.04  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2 取模运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //取摸运算本质  就是求余数\n",
    "    int a1 = 10;\n",
    "    int b1 = 3;\n",
    "\n",
    "    cout << a1 % b1 << endl;\n",
    "\n",
    "    /*两个数相除，除数不可以为0，所以也做不了取模运算\n",
    "    int a2 = 10;\n",
    "    int b2 = 0;\n",
    "\n",
    "    cout << a2 % b2 << endl;  //\n",
    "    */\n",
    "\n",
    "    /*两个小数是不可以做取模运算的\n",
    "    double d1 = 3.14;\n",
    "    double d2 = 1.1;\n",
    "\n",
    "    cout << d1 % d2 << endl;\n",
    "    */\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 1  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.3 递增递减运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //1、前置递增\n",
    "\n",
    "    int a = 10;\n",
    "    ++a;\n",
    "    cout << \"a=\" << a << endl;\n",
    "\n",
    "    //2、后置递增\n",
    "\n",
    "    int b = 15;\n",
    "    b++;\n",
    "    cout << \"b=\" << b << endl;\n",
    "\n",
    "    //3、前置和后置的区别\n",
    "    //前置递增，先让变量+1 然后进行表达式运算\n",
    "    int a2 = 10;\n",
    "    int b2 = ++a2 * 10;\n",
    "    cout << \"a2= \" << a2 << endl;\n",
    "    cout << \"b2= \" << b2 << endl;\n",
    "\n",
    "    //后置递增，先进行表达式运算，后让变量+1\n",
    "    int a3 = 10;\n",
    "    int b3 = a3++ * 10;\n",
    "    cout << \"a3= \" << a3 << endl;\n",
    "    cout << \"b3= \" << b3 << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - a=11  \n",
    " - b=16  \n",
    " - a2= 11  \n",
    " - b2= 110  \n",
    " - a3= 11  \n",
    " - b3= 100  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 赋值运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "int main()\n",
    "{\n",
    "\n",
    "    // = 赋值\n",
    "    int a = 10;\n",
    "    a = 100;\n",
    "    cout << \"a= \" << a << endl;\n",
    "\n",
    "    // += 加等于\n",
    "    int b = 10;\n",
    "    b = 10;\n",
    "    b += 2;\n",
    "    cout << \"b= \" << b << endl;\n",
    "\n",
    "    // -= 减等于\n",
    "    int c = 10;\n",
    "    c = 10;\n",
    "    c -= 2;\n",
    "    cout << \"c= \" << c << endl;\n",
    "\n",
    "    // *= 乘等于\n",
    "    int d = 10;\n",
    "    d = 10;\n",
    "    d *= 2;\n",
    "    cout << \"d= \" << d << endl;\n",
    "\n",
    "    // /= 除等于\n",
    "    int e = 10;\n",
    "    e = 10;\n",
    "    e /= 2;\n",
    "    cout << \"e= \" << e << endl;\n",
    "    \n",
    "    // %= 模等于\n",
    "    int f = 10;\n",
    "    f = 10;\n",
    "    f %= 2;\n",
    "    cout << \"f= \" << f << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - a= 100  \n",
    " - b= 12  \n",
    " - c= 8  \n",
    " - d= 20  \n",
    " - e= 5  \n",
    " - f= 0  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 比较运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 作用：用于表达式的比较，并返回一个真值或假值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //比较运算符\n",
    "    \n",
    "    // ==\n",
    "    int a = 10;\n",
    "    int b = 20;\n",
    "    cout << (a == b) << endl;\n",
    "\n",
    "    // !=\n",
    "    cout << (a != b) << endl;\n",
    "\n",
    "    // >\n",
    "    cout << (a > b) << endl;\n",
    "\n",
    "    // <\n",
    "    cout << (a < b) << endl;\n",
    "\n",
    "    // >=\n",
    "    cout << (a >= b) << endl;\n",
    "    \n",
    "    // <=\n",
    "    cout << (a <= b) << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 0  \n",
    " - 1  \n",
    " - 0  \n",
    " - 1  \n",
    " - 0  \n",
    " - 1  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 逻辑运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 作用：用于根据表达式的值返回真值或假值。\n",
    "\n",
    "1. !a 表示非，如果a为假，则!a为真；如果a为真，则!a为假。\n",
    "2. a&&b 表示与，如果a和b都为真，则结果为真，否则为假。\n",
    "3. a||b 表示或，如果a和b有一个为真，则结果为真，二者都为假时，结果为假。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //逻辑运算符\n",
    "\n",
    "    // !=\n",
    "    int a = 10;\n",
    "    int b = 30;\n",
    "\n",
    "    cout << (!a) << endl;  //在C++中除了0，都为真\n",
    "    cout << (!!a) << endl;\n",
    "\n",
    "    // &&\n",
    "    a = 10;\n",
    "    b = 30;\n",
    "\n",
    "    cout << (a && b) << endl;\n",
    "\n",
    "    a = 0;\n",
    "    b = 10;\n",
    "    cout << (a && b) << endl;\n",
    "\n",
    "    // ||\n",
    "    a = 10;\n",
    "    b = 30;\n",
    "    cout << (a || b) << endl;\n",
    "\n",
    "    a = 0;\n",
    "    b = 10;\n",
    "    cout << (a || b) << endl;\n",
    "\n",
    "    a = 0;\n",
    "    b = 0;\n",
    "    cout << (a || b) << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 0  \n",
    " - 1  \n",
    " - 1  \n",
    " - 0  \n",
    " - 1  \n",
    " - 1  \n",
    " - 0  \n",
    " - 请按任意键继续. . ."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
